package client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import lombok.extern.slf4j.Slf4j;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author yujie
 * @createTime 2023/2/23 16:05
 * @description
 */
@Slf4j
public class WsClient {

    public Channel getClient(){
        //websocke连接的地址，/hello是因为在服务端的websocket handler设置的
        URI websocketURI = null;
        try {
            websocketURI = new URI("ws://localhost:8010");
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders();
        //ws握手器
        WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(websocketURI, WebSocketVersion.V13, (String) null, true, httpHeaders);
        WebSocketClientHandler handler = new WebSocketClientHandler(handshaker);

        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap boot = new Bootstrap();
        boot.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        ChannelPipeline pipeline = socketChannel.pipeline();
                        pipeline.addLast("http-codec",new HttpClientCodec());
                        pipeline.addLast("aggregator",new HttpObjectAggregator(1024*1024*10));
                        pipeline.addLast("hookedHandler", handler);
                    }
                });

        Channel channel = null;
        try {
            channel = boot.connect(websocketURI.getHost(), websocketURI.getPort()).sync().channel();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //提前激活read尝试握手
        handshaker.handshake(channel);
        try {
            //阻塞等待是否握手成功
            handler.handshakeFuture().sync();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("当前客户端与服务端连接连接");

        //返回channel
        return channel;
    }

    public static void main(String[] args) {
//        Channel client = new WsClient().getClient();
//        new Message("client_req", "你是谁")
//        client.writeAndFlush()

    }

}
